package com.example.demo.controller;

import com.example.demo.model.BlogInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.BlogService;
import com.example.demo.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/blog")
    public List<BlogInfo> getAll(){
        return blogService.getAll();
    }

    @RequestMapping("/showblog")
    public BlogInfo showBlogByBlogId(Integer blogId){
        BlogInfo blogInfo = null;
        // 非空校验
        if(blogId == null || blogId < 0){
            return blogInfo;
        }
        blogInfo = blogService.showBlogByBlogId(blogId);
        return blogInfo;
    }

    @RequestMapping("/blogDelete")
    public int delBlog(Integer blogId){

        if(blogId==null){
            return  -1;
        }
        return blogService.delBlog(blogId);
    }

    @RequestMapping("/insert")
    public int insertBlog(String title,String content,HttpServletRequest request){
        HttpSession session = request.getSession(false);
        UserInfo userInfo =(UserInfo) session.getAttribute(ConstVariable.USER_SEESION_KEY);
        String postTime = "now()";
        blogService.insertBlog(title,content,userInfo.getUserId());
        return 1;
    }

}
